package com.fg.pro.ai.AiController;


import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.fg.pro.model.reponse.ResponseResult;
import com.fg.pro.model.utils.UserIdThread;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SafeGuardAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
//import org.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository;
import org.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "aiChat_JDBC_memory",description = "AiChat_JDBC_Memory")
@RestController
@RequestMapping("aiChat_JDBC_memory")
public class AiChat_JDBC_Memory {

    @Autowired
    DashScopeChatModel dashScopeChatModel;

    @Autowired
    private ChatMemory chatMemory;

    @Operation(summary = "默认回答（拦截器自动加入JDBC记忆体-不同用户）",description = "")
    @GetMapping("/chartByAdviceAndMemoryAndUser")
    public ResponseResult chartByAdviceAndMemoryAndUser(@RequestParam(required = false)String question){

        ChatClient client = ChatClient
                .builder(dashScopeChatModel)
//                .defaultAdvisors(
//                        PromptChatMemoryAdvisor.builder(chatMemory).build()
//                )
                .defaultAdvisors(
                        PromptChatMemoryAdvisor.builder(chatMemory)
                                .conversationId("123123") // 在builder中设置
                                .build()
                )
                .build();
        String content = client
                .prompt()
                .user(question)
//                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.DEFAULT_CONVERSATION_ID, "123123"))
                .call()
                .content();
        return ResponseResult.okResult(content);
    }

//    此配置，当前类生效
//    只能在test测试类中生效
    @Configuration
    static class Config{

//        配置记忆体
        @Bean
        ChatMemory chatMemory(JdbcChatMemoryRepository chatMemoryRepository){
            return MessageWindowChatMemory
                    .builder()
                    .maxMessages(3)
                    .chatMemoryRepository(chatMemoryRepository)
                    .build();
        }
    }



}
